import * as THREE from 'three';
import { modelAdd } from './modelOpera.js';

class RobotAdd {
  interval = null;
  curve = null
  model = null;
  progress = 0; // 物体运动时在运动路径的初始位置，范围0~1
  velocity = 0.001; // 影响运动速率的一个值，范围0~1，需要和渲染频率结合计算才能得到真正的速率
  yAxle = -30.5;
  step = [
    // 起点
    new THREE.Vector3(376, 130 + this.yAxle, -150),
    // 第一排横线
    new THREE.Vector3(376, 130 + this.yAxle, 240),
    // 左转角
    new THREE.Vector3(360, 130 + this.yAxle, 275),
    new THREE.Vector3(340, 130 + this.yAxle, 290),
    new THREE.Vector3(310, 130 + this.yAxle, 300),
    new THREE.Vector3(270, 130 + this.yAxle, 290),
    new THREE.Vector3(250, 130 + this.yAxle, 275),
    new THREE.Vector3(235, 130 + this.yAxle, 240),
    // 第二排横线
    new THREE.Vector3(234, 130 + this.yAxle, 220),
    new THREE.Vector3(234, 130 + this.yAxle, -220),
    // 右转角
    new THREE.Vector3(219, 130 + this.yAxle, -245),
    new THREE.Vector3(180, 130 + this.yAxle, -270),
    new THREE.Vector3(145, 130 + this.yAxle, -267),
    new THREE.Vector3(115, 130 + this.yAxle, -250),
    new THREE.Vector3(93, 130 + this.yAxle, -215),
    // 第三排横线
    new THREE.Vector3(93, 130 + this.yAxle, 240),
    // 左转角
    new THREE.Vector3(77, 130 + this.yAxle, 275),
    new THREE.Vector3(57, 130 + this.yAxle, 290),
    new THREE.Vector3(27, 130 + this.yAxle, 300),
    new THREE.Vector3(-13, 130 + this.yAxle, 290),
    new THREE.Vector3(-33, 130 + this.yAxle, 275),
    new THREE.Vector3(-48, 130 + this.yAxle, 240),
    // 第四排横线
    new THREE.Vector3(-49, 130 + this.yAxle, 220),
    new THREE.Vector3(-49, 130 + this.yAxle, -220),
    // 右转角
    new THREE.Vector3(-65, 130 + this.yAxle, -245),
    new THREE.Vector3(-104, 130 + this.yAxle, -270),
    new THREE.Vector3(-139, 130 + this.yAxle, -267),
    new THREE.Vector3(-169, 130 + this.yAxle, -250),
    new THREE.Vector3(-191, 130 + this.yAxle, -215),
    // 终点
    new THREE.Vector3(-191, 130 + this.yAxle, 220),
  ];

  constructor(scene) {
    this.makeCurve(scene, this.step);
    this.moveOnCurve();

    const trackData = [
      { url: './model/轨道直.fbx', num: 1, x: 376, y: 130, z: 10 },
      { url: './model/轨道左弯.fbx', num: 1, x: 305, y: 130, z: 300 },
      { url: './model/轨道直.fbx', num: 1, x: 234, y: 130, z: 10 },
      { url: './model/轨道右弯.fbx', num: 1, x: 163, y: 130, z: -270 },
      { url: './model/轨道直.fbx', num: 1, x: 92, y: 130, z: 10 },
      { url: './model/轨道左弯.fbx', num: 1, x: 21, y: 130, z: 300 },
      { url: './model/轨道直.fbx', num: 1, x: -50, y: 130, z: 10 },
      { url: './model/轨道右弯.fbx', num: 1, x: -121, y: 130, z: -270 },
      { url: './model/轨道直.fbx', num: 1, x: -192, y: 130, z: 10 },
    ];
    new modelAdd(trackData, (group) => {
      scene.add(group);
    });
    const robotData = [
      { url: './model/机器人.fbx', groupName: '机器人组', name: '机器人', num: 1, x: 0, y: 0, z: 0 },
      { url: './model/机器人铁大logo.fbx', name: '机器人LOGO正', num: 1, x: 0, y: 9.5, z: 9.5 },
      { url: './model/机器人铁大logo.fbx', name: '机器人LOGO反', num: 1, x: 0, y: 9.5, z: -11.5, rotateY: Math.PI },
    ];
    new modelAdd(robotData, (group) => {
      group.rotateY(Math.PI);
      group.position.set(375.7, 99.5, -150);
      scene.add(group);
      this.model = group;
    });

    document.addEventListener('click', (e) => {
      const ev = e || window.event;
      ev.preventDefault();
      // let step = [];

      switch (ev.target.id) {
        case 'Robot1':
          // if (this.progress > 0.09) {
          //   for (const item of this.step) {
          //     step.unshift(item)
          //   }
          //   this.makeCurve(scene, step);
          // } else {
          //   step = this.step;
          // }
          this.run(0.09);
          break;
        case 'Robot2':
          // if (this.progress > 0.29) {
          //   for (const item of this.step) {
          //     step.unshift(item)
          //   }
          //   this.makeCurve(scene, step);
          // } else {
          //   step = this.step;
          // }
          this.run(0.29);
          break;
        case 'Robot3':
          this.run(0.54);
          break;
        case 'Robot4':
          this.run(0.97);
          break;
      }
    });
  }
  makeCurve(scene, step) {
    this.curve = new THREE.CatmullRomCurve3(step);
    this.curve.curveType = "catmullrom";
    // curve.closed = true;// 设置是否闭环
    this.curve.tension = 0;// 设置线的张力，0为无弧度折线

    // 为曲线添加材质在场景中显示出来，不显示也不会影响运动轨迹，相当于一个Helper
    const points = this.curve.getPoints(50);
    const geometry = new THREE.BufferGeometry().setFromPoints(points);
    // const geometry = new THREE.BufferGeometry();
    const material = new THREE.LineBasicMaterial({ color: 0x000000 });
    const curveObject = new THREE.Line(geometry, material);
    scene.add(curveObject);
  }
  moveOnCurve() {
    if (this.curve && this.model) {
      if (this.progress <= 1 - this.velocity) {
        const point = this.curve.getPointAt(this.progress);// 获取样条曲线指定点坐标
        const pointBox = this.curve.getPointAt(this.progress + this.velocity);// 获取样条曲线指定点坐标

        if (point && pointBox) {
          this.model.position.set(point.x, point.y, point.z);
          var targetPos = pointBox;// 目标位置点
          var offsetAngle = 0;// 目标移动时的朝向偏移

          // 以下代码在多段路径时可重复执行
          var mtx = new THREE.Matrix4();// 创建一个4维矩阵
          mtx.lookAt(this.model.position, targetPos, this.model.up);// 设置朝向
          mtx.multiply(new THREE.Matrix4().makeRotationFromEuler(new THREE.Euler(0, offsetAngle, 0)))
          var toRot = new THREE.Quaternion().setFromRotationMatrix(mtx);// 计算出需要进行旋转的四元数值
          this.model.quaternion.slerp(toRot, 0.2)
        }
        this.progress += this.velocity;
      }
      else {
        this.progress = 0;
      }
    }
  }
  run(count) {
    this.interval = setInterval(() => {
      if (this.progress > count) {
        clearInterval(this.interval);
      } else {
        this.moveOnCurve();
      }
    }, 50);
  }
}
export { RobotAdd }; 